// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-01

package qrcode

import (
	"context"
	"gitee.com/go-libs/result"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib/authorizer"
)

type Logic struct {
	auth     *authorizer.Authentication
	response *Response
}

func New() logics.Logical {
	return &Logic{
		response: &Response{},
	}
}

func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	o.auth = authorizer.New().Get(i)
	return
}

func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err   error
		model *models.Qrcode
		res   = result.New()
	)

	// 1. 申请出错.
	if model, err = services.NewQrcodeService().Apply(ctx, kind.QrcodeTypeUser, o.auth.UserId, map[string]any{
		"Id": o.auth.UserId,
	}); err != nil {
		return res.WithError(err)
	}

	// 2. 申请成功.
	return res.With(o.response.With(model))
}
